

jQuery(function ($) {
  var user = JSON.parse(localStorage.getItem('user_info')) || {};
  var token = user.token;
  var reg = /^1[3456789]\d{9}$/;
  var reg_email = /^\w+\@+[0-9a-zA-Z]+\.(com|com.cn|edu|hk|cn|net)$/;
  //禁止输入中文
  // var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g
  $('#tel').on('input', function () {
    var value = $(this).val();
    var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
    this.value = value.replace(re, '');
  });

  //判断格式
  $("#tel").blur(function () {
    var reg = /^1[3456789]\d{9}$/;
    var reg_email = /^\w+\@+[0-9a-zA-Z]+\.(com|com.cn|edu|hk|cn|net)$/;
    if (reg.test($("#tel").val()) || reg_email.test($("#tel").val())) {
      if (reg_email.test($("#tel").val())) {
        $('#input-box').hide();
      } else {
        $('#input-box').show();
      }
      $(".tel-msg").text('');
      $('#input_code').removeClass('hide');
      change_pic();
    } else {
      $(".tel-msg").text('请输入正确的手机号或邮箱地址');
      $('#input_code').addClass('hide');
      $('#text').val('');
      return;
    }
    if ($("#tel").val() == '') {
      $(".tel-msg").text('对不起，账号不能为空');
      $('#input_code').addClass('hide');
      $('#text').val('');
      return;
    }
  });
  //密码不能少于 六位数
  $("#code").blur(function () {
    var password = $.trim($("#code").val());
    if (password.length == 0) {
      $(".code-msg").html("请输入密码");
      return;
    } else {
      $(".code-msg").html("");
    }
  });

  //忘记密码
  $("#btn").click(function () {
    var loginCode = $.trim($("#tel").val()); //账号
    var password = $.trim($("#code").val()); //密码
    var password2 = $.trim($("#code2").val()); //密码
    var textCode2 = $.trim($('#text2').val()); //短信验证码
    var tel_parm = {
      "phone": loginCode,
      "password": password,
      "code": textCode2
    };
    var email_parm = {
      "email": loginCode,
      "password": password
      //非空验证
    };
    if (loginCode == "" || loginCode == null) {
      $(".tel-msg").html("对不起，账号不能为空");
      return;
    } else if (password == "" || password == null) {
      $("#code").focus();
      $(".code-msg").html("对不起，登录密码不能为空");
      return;
    } else if (textCode2 == "" || textCode2 == null) {
      $(".text-msg").html("请输入验证码");
      return;
    }

    if (reg.test($("#tel").val()) && password == password2) {

      if (textCode2 == "" || textCode2 == null) {
        $(".text-msg2").html("请输入验证码");
        return;
      }

      my_ajax('/api/user/forget_phone', tel_parm, function succCallback(result) {
        window.location.href = "reset_pw_success.html";
      });
    } else if (reg_email.test($("#tel").val()) && password == password2) {

      my_ajax('/api/user/forget_email', email_parm, function succCallback(result) {
        window.location.href = "mail_verify.html?type=2&&email=" + loginCode;
      });

      //1是注册成功
      //2是邮箱重置密码成功     
    }
  });
  $('#change_pic').click(function () {
    change_pic();
  });
  function change_pic() {
    var tel = $.trim($("#tel").val()); //账号
    var img = document.querySelector('.text_pic');
    img.src = host + "/api/code/img?phone=" + tel;
  }
  function my_ajax(url, data, succCallback) {
    return $.ajax({
      type: 'POST', //提交方法是POST
      url: host + url, //请求的路径
      data: JSON.stringify(data),
      contentType: "application/json",
      xhrFields: {
        withCredentials: true
      },
      beforeSend: function beforeSend(request) {
        request.setRequestHeader("X-AUTH-TOKEN", token);
      },
      success: function success(result) {
        if (result.code == 200) {
          if (succCallback) {
            succCallback(result);
          }
        } else {
          $(".tel-msg").html(result.msg);
          change_pic();
        }
      },
      error: function error(_error) {
        change_pic();
      }
    });
  }
  var time = 60;
  var flag = true;
  var timer;
  $('#btn-code').click(function () {
    //获取手机验证码
    var tel = $.trim($("#tel").val());
    var textCode = $.trim($('#text').val()); //图形验证码
    if (textCode == "" || textCode == null) {
      $(".tel-msg").html("请输入验证码！");
      return;
    };
    if (!tel) {
      $(".tel-msg").html("对不起，账号不能为空");
      return;
    } else if (!reg.test($("#tel").val())) {
      $(".tel-msg").text('请输入正确的手机号或邮箱地址');
      return;
    } else {
      $(".tel-msg").text('');
      var data = {
        phone: tel,
        imgCode: textCode,
        type: 1
      };
      $.ajax({
        type: 'POST', //提交方法是POST
        url: host + '/api/code/sms', //请求的路径
        data: JSON.stringify(data),
        contentType: "application/json",
        beforeSend: function beforeSend(request) {
          request.setRequestHeader("X-AUTH-TOKEN", token);
        },
        error: function error(_error2) {
          //请求失败的回调方法 
          alert('网络加载失败，请稍候再试!');
        },
        success: function success(result) {
          if (result.code == 200) {
            $("#btn-code").attr("disabled", true);
            if (flag == true) {
              flag = false;
              timeone();
            }
          } else {
            $(".tel-msg").html(result.msg);
          }
        }
      });
    }
  });
  function timeone() {
    $('#btn-code').html("(" + time + ")后重新发送");
    timer = setInterval(function () {
      time--;
      if (time == 0) {
        $("#btn-code").attr("disabled", false);
        $("#btn-code").html("点击获取验证码");
        time = 60;
        flag = true;
        clearInterval(timer);
      } else {
        $('#btn-code').html("(" + time + ")后重新发送");
      }
    }, 1000);
  };

  $(document).keydown(function (event) {
    if (event.keyCode == 13) {
      $('#btn').triggerHandler('click');
    }
  });
});